package main

import (
	_ "embed"
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"github.com/lxn/win"
	"log"
	"os/exec"
	"strconv"
	"strings"
)

type MyMainWindow struct {
	*walk.MainWindow
	Input *walk.TextEdit
	Btn   *walk.PushButton
}

func main() {
	mv := &MyMainWindow{
		Input: &walk.TextEdit{},
		Btn:   &walk.PushButton{},
	}
	err := (MainWindow{
		Title:    "微信多开",
		AssignTo: &mv.MainWindow,
		Layout:   VBox{},
		Children: []Widget{
			GroupBox{
				Layout: HBox{},
				Children: []Widget{
					Label{
						Text: "请输入启动微信个数",
					},
					TextEdit{
						AssignTo: &mv.Input,
					},
				},
			},
			PushButton{
				AssignTo:  &mv.Btn,
				Text:      "启动",
				OnClicked: mv.StartWechat,
			},
		},
	}.Create())
	if err != nil {
		log.Fatalln(err)
	}
	mv.SetHeight(100)
	mv.SetWidth(300)
	// 设置窗体在屏幕的正中间
	mv.SetX((int(win.GetSystemMetrics(0)) - mv.Width()) / 2)
	mv.SetY((int(win.GetSystemMetrics(1)) - mv.Height()) / 2)

	mv.Run()
}

func (m *MyMainWindow) StartWechat() {
	txt := m.Input.Text()
	num, err := strconv.Atoi(txt)
	if err != nil {
		walk.MsgBox(m, "提示", "微信启动个数必须为整数", walk.MsgBoxIconInformation)
	}
	go func() {
		defaultExec := "start D:/programs/WeChat/WeChat.exe"
		execSlice := []string{}
		for i := 0; i < num; i++ {
			execSlice = append(execSlice, defaultExec)
		}
		command := strings.Join(execSlice, " | ")
		exec.Command(`cmd`, `/c`, command).Start()
	}()
}
